<script type="text/javascript">
var scriptLibraries = MyOpenSpace.ClientLibraries.Scripts;
var includeLib = MyOpenSpace.ClientLibraries.includeScript;

//includeLib(scriptLibraries.PROTOTYPE);
includeLib(scriptLibraries.MYSPACE_WIDGETS);

</script>


<div style="background:#EEEEEE;color:#333333;height:100%;width:100%;padding:5px;overflow:scroll;">

<style type="text/css">
	#log{
		float:left;
		margin:5px;
		padding:5px;
		border:5px solid #888;
		width:350px;
		height:350px;
		overflow:auto;
		background:white;
	}
	div.test{
		margin:10px;
		margin-bottom:25px;
		padding:5px;
		border:3px solid #AA3333;
		padding-top:0;
	}
	
	div.test h2{
		padding:5px;
		margin:0;
		margin-right:150px;
		font-family:Arial;
		font-weight:bold;
		background:#CCC;
		color:#333;
		font-size:12pt;
		border-bottom:1px dashed blue;
	}	

	div.test .teststatus{
		float:right;
		width:50px;
		padding:8px;
	}
	
	.pass{
		color: #CCCCCC;
		background: green;
	}
	.fail{
		color: white;
		background: #AA2222;
	}
	.incomplete{
		color: black;
	}
.label{
	float:left;
	width:100px;
}
.labeled{
	margin-left:110px;
}
	
</style>

<script type="text/javascript">
window.osContainer = opensocial.Container.get();

if(!window.$){
	window.$ = document.getElementById;
}


function loadFriendPicker(){

window.setTimeout(function(){
	MyOpenSpace.Widgets.Bootstrapper.createWidget("MyOpenSpace.Widgets.FriendPicker", function(p){
		window.friendPicker = p;
	}, {
		element: "pickerDiv01",
		buildSelectedUI: true
	});
}, 200);
}


</script>

<script type="text/javascript">

var DATAKEY = "datakey";

function setDataCallback(data){
	gadgets.log("-- Received setdata Response --");
//  debugger;
	var rsp = data.get("setkey");
	if(data.hadError()){
		gadgets.log("Set Error");
		gadgets.log(rsp.getErrorMessage());
	}
}

function getMyAppDataResponse(data){
	gadgets.log("Got data");
  debugger;
	var rsp = data.get(freqKey);
	if(data.hadError()){
		gadgets.log("Set Error");
		gadgets.log(rsp.getErrorMessage());
	}
	else{
		gadgets.log("Got: " + rsp.getData());
	}
}


function setAppDataKey(key, value) {
	var idparams = {};
	idparams[opensocial.IdSpec.Field.USER_ID] = opensocial.IdSpec.PersonId.VIEWER;
	idparams[opensocial.IdSpec.Field.NETWORK_DISTANCE] = 0;	
//	idparams[opensocial.IdSpec.Field.GROUP_ID] = opensocial.IdSpec.GroupId.SELF
	//idparams[opensocial.IdSpec.Field.USER_ID] = opensocial.IdSpec.PersonId.VIEWER;
	
	
	var id = opensocial.newIdSpec(idparams);
	//debugger;
  var jval = gadgets.json.stringify(value);
// var jval = {"x" : "zoo"};
  var req = opensocial.newDataRequest();
  req.add(req.newUpdatePersonAppDataRequest(opensocial.IdSpec.PersonId.VIEWER, key, jval), "setkey");
  req.send(setDataCallback);
};

gadgets.log = function(msg){
	var el = document.getElementById("log");
	var m = document.createElement("div");
	m.innerHTML = msg;
	el.appendChild(m);
}


function setThisData(){
	var el = document.getElementById("mynewdata");
	setAppDataKey(DATAKEY, el.value);
}

var freqKey = "freq";


function getThisData(){
	var idparams = {};
	idparams[opensocial.IdSpec.Field.USER_ID] = opensocial.IdSpec.PersonId.VIEWER;
	idparams[opensocial.IdSpec.Field.NETWORK_DISTANCE] = 0;	
	idparams[opensocial.IdSpec.Field.GROUP_ID] = opensocial.IdSpec.GroupId.SELF;

	var id = opensocial.newIdSpec(idparams);
	//debugger;
 var req = opensocial.newDataRequest();
  req.add(req.newFetchPersonAppDataRequest(id, "*"), freqKey);
  req.send(getMyAppDataResponse);	
	
}


function getFriendData(){
debugger;
	var idparams = {};
//	idparams[opensocial.IdSpec.Field.USER_ID] = opensocial.IdSpec.PersonId.VIEWER;
//	idparams[opensocial.IdSpec.Field.NETWORK_DISTANCE] = 1;	
	idparams[opensocial.IdSpec.Field.USER_ID] = window.friendPicker.selectedFriend.getId();
	idparams[opensocial.IdSpec.Field.GROUP_ID] = "@self"; // opensocial.IdSpec.GroupId.SELF;
	//idparams[opensocial.IdSpec.Field.GROUP_ID] = opensocial.IdSpec.GroupId.FRIENDS;
//	idparams[opensocial.IdSpec.Field.GROUP_ID] = window.friendPicker.selectedFriend.getId();

	var id = opensocial.newIdSpec(idparams);
  var req = opensocial.newDataRequest();
  req.add(req.newFetchPersonAppDataRequest(id, "*"), freqKey);
  req.send(getFriendAppDataResponse);	
}

function getFriendAppDataResponse(dataResponse){
	debugger;
	if(dataResponse.hadError()){
		gadgets.log("Err: " + dataResponse.getError());
		return;
	}
	var friendId = window.friendPicker.selectedFriend.getId();
	var id = friendId.split(":")[1];
	var data = dataResponse.get(freqKey);
	var y = data.getData();
	
	var friendData = y[id];
	if(friendData){
		gadgets.log("Val for : " + window.friendPicker.selectedFriend.getDisplayName() + " is " + friendData[DATAKEY] )
	}
	else{
		gadgets.log("Bad request");
	}
/*
	for (tmp in y){
		gadgets.log("Val: " + tmp + " is " + y[tmp] )
		
	}
*/

}

function clearLog(){
	var el = document.getElementById("log");
	el.innerHTML = "";	
}

</script>

<img src="http://a118.ac-images.myspacecdn.com/images01/76/l_ad8e94921ecbd130e59980e4459c152d.png" 
	style="width:64px;height:64px;border:2px solid green;-moz-border-radius:1.5em;padding:2px;background:white;"
	width="64" height="64" align="left" alt="Nefarious Skelanimals" />
<!--
http://www.skelanimals.com/ <br/>
http://www.myspace.com/skelanimal
-->
<h2 style="font-family:android, comic sans ms, courier;">
Shared AppData Sample</h2>

<div id="log"> </div>


<div class="test">
		<div>
		<input type="text" id="mynewdata">
		
		<button onclick="setThisData()" >Set Data</button>
		<button onclick="getThisData()" >Get Test Data</button>		
		</div>

		<div>
			<div class="label">Friend</div>
			<div class="labeled"><div id="pickerDiv01"> </div></div>
			<br />
		<button onclick="getFriendData()" >Get Friend Data</button>			
		</div>
	
	
</div>


		<button onclick="clearLog()" >Clear Log</button>			


	<script type="text/javascript">

gadgets.util.registerOnLoadHandler(loadFriendPicker);

	</script>
	

	
</div>

